package com.ziyun.erp.modules.userTeam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.service.utils.sms.SmsResponse;
import com.service.utils.sms.SmsUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.userTeam.entity.UserDistributionEntity;
import com.ziyun.erp.modules.userTeam.entity.UserTeamVo;
import com.ziyun.erp.modules.userTeam.service.UserDistributionService;
import io.renren.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.userTeam.dao.UserTeamDao;
import com.ziyun.erp.modules.userTeam.entity.UserTeamEntity;
import com.ziyun.erp.modules.userTeam.service.UserTeamService;
import org.springframework.transaction.annotation.Transactional;


@Service("userTeamService")
public class UserTeamServiceImpl extends ServiceImpl<UserTeamDao, UserTeamEntity> implements UserTeamService {

    @Autowired
    private UserDistributionService userDistributionService;

    @Override
    public PageUtils list(Map<String, Object> params) {
        Page<UserTeamVo> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));
        List<UserTeamVo> list = this.baseMapper.list(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R audit(UserTeamVo userTeamVo) {
        UserTeamEntity userTeam = new UserTeamEntity();
        userTeam.setTeamId(userTeamVo.getTeamId());
        userTeam.setAuditStatus(1);
        this.baseMapper.updateById(userTeam);
        //update分销表apply_type
        UserDistributionEntity userDistributionEntity = userDistributionService.selectOne(new EntityWrapper<UserDistributionEntity>().eq("user_id", userTeamVo.getUserId()).eq("source_type", userTeamVo.getSourceType()));
        userDistributionEntity.setApplyType("added");
        userDistributionEntity.setVerifyTime(new Date());
        userDistributionEntity.setVerifyUser(UserUtils.getCurentUser().getUsername());
        userDistributionService.updateById(userDistributionEntity);

        // 发送短信
        JSONObject smsJson = new JSONObject();
        smsJson.put("phone", userTeamVo.getMobile());
        SmsResponse smsResponse = SmsUtils.sendTeamAuditPassed(userTeamVo.getMobile(), smsJson.toString());
        if (StringUtils.equals(smsResponse.getCode(), "OK")) {
            return R.ok("审核成功,短信已发送!");
        } else {
            return R.ok("审核成功,短信发送失败!");
        }
    }

    @Override
    @Transactional
    public R pass(UserTeamVo userTeamVo) {
        UserTeamEntity userTeam = new UserTeamEntity();
        userTeam.setTeamId(userTeamVo.getTeamId());
        userTeam.setAuditStatus(2);
        this.baseMapper.updateById(userTeam);
        //update分销表apply_type
        UserDistributionEntity userDistributionEntity = userDistributionService.selectOne(new EntityWrapper<UserDistributionEntity>().eq("user_id", userTeamVo.getUserId()).eq("source_type", userTeamVo.getSourceType()));
        //(check:申请加入,,added:已加入,applyOut:申请退出,leave:已退出, refused:已拒绝)
        userDistributionEntity.setApplyType("refused");
        userDistributionEntity.setRoleId(0);
        userDistributionEntity.setTeamId(0);
        userDistributionEntity.setRefuseReason(userTeamVo.getRefuseReason());
        userDistributionService.updateById(userDistributionEntity);

        // 发送短信
        JSONObject smsJson = new JSONObject();
        smsJson.put("phone", userTeamVo.getMobile());
        SmsResponse smsResponse = SmsUtils.sendTeamAuditFailed(userTeamVo.getMobile(), smsJson.toString());
        if (StringUtils.equals(smsResponse.getCode(), "OK")) {
            return R.ok("审核拒绝,短信已发送!");
        } else {
            return R.ok("审核拒绝,短信发送失败!");
        }
    }

}
